package com.base.cn.platform.os.controller.manage.user;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.function.SysFunctionService;
import com.base.cn.platform.os.service.manage.role.SysRoleService;
import com.base.cn.platform.os.service.manage.sysUser.SysUserFunctionService;
import com.base.cn.platform.os.service.manage.sysUser.SysUserLoginService;
import com.base.cn.platform.os.service.manage.sysUser.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员用户Controller
 *
 * @author s.li
 * @create 2018-02-07 16:20
 */
@Controller
@RequestMapping("/admin/manage/user")
public class AdminSysUserController extends AdminBaseController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserLoginService sysUserLoginService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysFunctionService sysFunctionService;
    @Autowired
    private SysUserFunctionService sysUserFunctionService;

    /**
     * 初始化更新、添加用户页面
     * @param userId 用户ID
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "userId",required = false,defaultValue = "0")BigDecimal userId){

        Map<String,Object> sysUser = null;
        if(userId.compareTo(new BigDecimal(0))>0){
            sysUser = sysUserService.findSysUserById(userId,true,true,false);
        }
        if(sysUser==null){
            sysUser = new HashMap<>();
        }
        model.addAttribute("sysUser",sysUser);
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        params.put("notIds","1");
        List<Map<String,Object>> roleList = sysRoleService.findSysRoleListCondition(params);
        model.addAttribute("roleList",roleList);
        return resultPage("/manage/user/save_sys_user");
    }

    /**
     * 创建用户
     * @return Map<String,Object>
     */
    @RequestMapping("/saveSysUser")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "创建或者修改后台用户")
    public Map<String,Object> saveSysUser(HttpServletRequest request){
        //用户数据
        Map<String,Object> sysUser = this.getParamsMap(request);
        sysUser.put("regType",1);//标记为后台开通，2是自申请
        sysUser.put("createUserId",loginUtil.getLoginUserId(request));
        return sysUserLoginService.executeRegister(sysUser);
    }

    /**
     * 获取用户数据列表
     * @param model
     * @return 操作页面
     */
    @RequestMapping("/querySysUserList")
    public String querySysUserList(Model model){
        model.addAttribute("count",sysUserService.count());
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        List<Map<String,Object>> roleList = sysRoleService.findSysRoleListCondition(params);
        model.addAttribute("roleList",roleList);
        return resultPage("/manage/user/sys-user-list");
    }

    /**
     * 加载用户列表
     * @param model
     * @param currentPage
     * @param params
     * @return
     */
    @RequestMapping("/ajaxQueryUserList")
    public String ajaxQueryUserList(Model model,
                                    @RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage,
                                    @RequestBody Map<String,Object> params){
        Map<String,Object> dataMap = sysUserService.querySysUserList(currentPage,params,true,true,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/manage/user/ajax-sys-user-list");
    }

    /**
     * 修改用户状态
     * @param userIds 用户ID串
     * @param status 状态，1正常，2禁用，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateSysUserStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "修改后台用户账号状态")
    public Map<String,Object> updateSysUserStatus(@RequestParam("userIds") String userIds,
                                                  @RequestParam("status") int status){
        Map<String,Object> resultMap = null;
        if(status==3){
            //删除用户(以下代码并非逻辑问题目前系统用户的删除改为假删除,后期会修改)
            //resultMap = sysUserService.deleteSysUser(userIds);真删除
            resultMap = sysUserService.updateSysUserStatus(userIds,status);
            resultMap.put("message","用户删除成功");
        }else {//非删除用户
            resultMap = sysUserService.updateSysUserStatus(userIds,status);
        }
        return resultMap;
    }

    /**
     * 延期体验用户过期时间
     * @param userIds
     * @return Map<String,Object>
     */
    @RequestMapping("/delayedUser")
    @ResponseBody
    public Map<String,Object> delayedUser(@RequestParam("userIds") String userIds){
        return sysUserService.delayedUser(userIds);
    }

    /**
     * 修改密码
     * @param userId 用户ID
     * @param newPassword 新的密码
     * @return Map<String,Object>
     */
    @RequestMapping("/updateUserPwd")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "修改后台用户账号密码")
    public Map<String,Object> updateUserPwd(@RequestParam("userId") BigDecimal userId,
                                            @RequestParam("newPassword") String newPassword){
        return sysUserService.updateUserPwd(userId,newPassword);
    }

    /**
     * 查看用户详情
     * @param userId 用户ID
     * @return 显示页面
     */
    @RequestMapping("/showUserInfo")
    public String showUserInfo(Model model,
                               @RequestParam("userId") BigDecimal userId){
        Map<String,Object> sysUser = sysUserService.findSysUserById(userId,true,true,true);
        model.addAttribute("sysUser",sysUser);
        List<Map<String,Object>> sysFunctionList = sysFunctionService.findNotCacheUserFunctionList(userId,true);
        model.addAttribute("sysFunctionList",sysFunctionList);
        return resultPage("/manage/user/sys_user_info");
    }

    /**
     * 选择用户列表
     * @return 操作页面
     */
    @RequestMapping("/showSelectUserList")
    public String showSelectUserList(Model model,
                                     @RequestParam(value = "selectType",required = false,defaultValue = "radio") String selectType){
        model.addAttribute("selectType",selectType);
        return resultPage("/manage/user/select-sys-user-list");
    }

    /**
     * 加载用户列表
     * @param model
     * @param currentPage
     * @param params
     * @return
     */
    @RequestMapping("/ajaxSelectQueryUserList")
    public String ajaxSelectQueryUserList(Model model,
                                    @RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage,
                                    @RequestBody Map<String,Object> params){
        Map<String,Object> dataMap = sysUserService.querySysUserList(currentPage,params,true,true,true);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("selectType",params.getOrDefault("selectType", "radio"));
        return resultPage("/manage/user/select-ajax-sys-user-list");
    }

    /**
     * 初始化用户授权页面
     * @param userId 用户ID
     * @return 操作页面
     */
    @RequestMapping("/initUserAuthorization")
    public String initUserAuthorization(Model model,
                                        @RequestParam("userId") BigDecimal userId){
        model.addAttribute("userId",userId);
        return resultPage("/manage/user/individual-function");
    }

    /**
     * 加载权限
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/ajaxSysFunctionList")
    @ResponseBody
    public Map<String,Object> ajaxSysFunctionList(@RequestParam("userId") BigDecimal userId){
        List<Map<String,Object>> sysFunctionList = sysUserFunctionService.findAllFunction(userId);
        return ResultUtil.SUCCESS(sysFunctionList);
    }

    /**
     * 保存用户权限
     * @param userId 用户ID
     * @param functionIds 权限ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/saveUserFunction")
    @ResponseBody
    public Map<String,Object> saveUserFunction(@RequestParam("userId") BigDecimal userId,
                                               @RequestParam(value = "functionIds",required = false,defaultValue = "") String functionIds){
        return sysUserFunctionService.saveUserFunction(userId,functionIds);
    }
}
